package com.codingvip;

import org.apache.zookeeper.*;

public class ZkTest {
    public static void main(String[] args) throws Exception {
        ZkTest zkTest = new ZkTest();
        zkTest.init();
        zkTest.testCreate();

    }

    //这里是服务器里安装的ZK，conf -> zoo.cfg 里自己配的 server
    private static final String connectString = "192.168.95.125:2181,192.168.95.125:2181,192.168.95.52:2181";
    private static final int sessionTimeout = 2000;

    ZooKeeper zkClient = null;

    public void init() throws Exception {
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                //收到事件通知后的回调函数（应该是我们自己的事件处理逻辑）
                System.out.println(event.getType() + "----" + event.getPath());
                try {
                    zkClient.getChildren("/", true);//再次触发监听
                } catch (Exception e) {
                }
            }
        });
    }

    /**
     * 创建数据节点到zk中
     * 描述:在zk的根路径下创建一个节点eclipse，内容为hellozk，
     * 第三个参数是权限，我们给的是Ids.OPEN_ACL_UNSAFE 意思是 This is a completely open ACL .
     */
    public void testCreate() throws Exception {
        String create = zkClient.create("/eclipse", "hellozk".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        System.out.println("创建----" + create);
    }


}

